package cn.iune.web.erp.controllers;

import java.math.BigDecimal;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfWriter;
import com.justep.baas.Utils;

import cn.iune.backcenter.author.sysinfo.ISysInfoService;
import cn.iune.backcenter.author.sysinfo.SysInfo;
import cn.iune.common.response.MapMessage;
import cn.iune.config.SecurityService;
import cn.iune.erp.inventory.IStorageRecordService;
import cn.iune.erp.inventory.StorageRecord;
import cn.iune.erp.invoice.AccountInvoice;
import cn.iune.erp.invoice.IInvoiceService;
import cn.iune.erp.sale.ISaleOrderService;
import cn.iune.erp.sale.ISaleService;
import cn.iune.erp.sale.SaleOrder;

@Controller
@RequestMapping(value="/sale")
public class SaleController {
	
	@Autowired
	private ISaleOrderService saleOrderService;
	@Autowired
	private ISaleService saleService;
	
	@Autowired
	private IStorageRecordService storageRecordService;
	
	@Autowired
	private IInvoiceService invoiceService;
	
	@Autowired
	private ISysInfoService sysInfoService;
	
	
	
	
	
	
	
	
	
	


	/*
	@RequestMapping(value="/saleForecast/single/delete",method=RequestMethod.GET)
	@ResponseBody
	public void saleForecastDelete(@RequestParam(value="id",required=true) String id) {
		saleService.saleForecastDelete(id);
	}
	*/

	

	//////////////////////////////////////////////////////////////////////////////////////
	/*
	@RequestMapping(value={"/diningOrderDetail/single/confirm"},method=RequestMethod.GET)
	@ResponseBody
	public int saleForecastDetailConfirm(@RequestParam(value="id",required=true) String id) {
		return saleForecastDetailService.confirmByMasterID(id);
	}
	*/

	
	
	/*
	@RequestMapping(value={"/diningOrderDetail/single/agreeIt"},method=RequestMethod.GET)
	@ResponseBody
	public int saleForecastDetailAgreeIt(@RequestParam(value="id",required=true) String id) {
		return saleForecastDetailService.agreeItByMasterID(id);
	}
	*/
	
	

	/*
	@RequestMapping(value="/saleOffer/single/delete",method=RequestMethod.GET)
	@ResponseBody
	public void saleOfferDelete(@RequestParam(value="id",required=true) String id) {
		saleService.saleOfferDelete(id);
	}
	*/

	


	//////////////////////////////////////////////////////////////////////////////////////

	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	//销售订单（工厂使用，批发零售均可）
//	@RequestMapping(value="/saleOrder/single",method=RequestMethod.GET, headers="Referer=http://www.ifeng.com/")
	@RequestMapping(value="/saleOrder/single",method=RequestMethod.GET)
	public ModelAndView saleOrderSingle(){
		ModelAndView mav = new ModelAndView("/appcenter/sale/saleOrder/single");
		mav.addObject("path", "/sale/saleOrder/single");
		return mav;
	}
	
	

	@RequestMapping(value={
			"/saleOrder/single/query"
//			,"/saleOrder/query"
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject saleOrderQuery(HttpServletRequest request, @RequestBody JSONObject data) {
		/*
		System.out.println("data-->"+data.toJSONString());
		//获取访问路径
		String url = request.getRequestURI().replaceAll(request.getContextPath(), "").replaceAll("/query", "");
		System.out.println("request.getRequestURI()-->"+request.getRequestURI());
		System.out.println("url-->"+url);
		//判断是否具有只查看自己的条件
		if("Y".equals(SecurityService.getDataAccessPerm(url))){
			data.put("userId", SecurityService.getCurrentPerson().getsID());
		}
		
		String enterpriseNo = SecurityService.getCurrentOgn().getsFID();
		data.put("enterpriseNo", enterpriseNo);
		*/
		data = SecurityService.addDataAccessPerm(data);
		return saleOrderService.queryData(data);
	}

	@RequestMapping(value={
			"/saleOrder/single/save"
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject saleOrderSave(@RequestBody JSONObject object) {
		return saleOrderService.saveData(object);
		
	}

	/*
	@RequestMapping(value="/saleOrder/single/delete",method=RequestMethod.GET)
	@ResponseBody
	public void saleOrderDelete(@RequestParam(value="id",required=true) String id) {
		saleService.saleOrderDelete(id);
	}
	*/

	@RequestMapping(value={
			"/saleOrder/single/delete"
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject saleOrderDelete(@RequestBody JSONObject data) {
		return saleOrderService.deleteData(data);
	}

	@RequestMapping(value={
			"/saleOrder/single/create"
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject saleOrderCreate(@RequestBody JSONObject data) {
		System.out.println("controller.data-->>"+data.toJSONString());
		JSONObject params = JSONObject.parseObject(data.toJSONString());
		//增加登录信息
		//缺省值
		JSONArray defaultValues = params.getJSONArray("defaultValues");
		//循环缺省值（多行）
		for(int i=0;i<defaultValues.size();i++) {
			JSONObject rowValue = defaultValues.getJSONObject(i);
			//循环列
			//循环缺省值（单行）
			for(String col: rowValue.keySet()) {
				System.out.println("controller.defaultValues-->>"+col+":"+rowValue.get(col));
				if(":getCurrentDate()".equals(rowValue.get(col))) {
					rowValue.put(col, Utils.getCurrentDate());
				}
				if(":getCurrentPsmFID()".equals(rowValue.get(col))) {
					rowValue.put(col, SecurityService.getCurrentPsm().getsFID());
				}
				if(":getCurrentPsmFName()".equals(rowValue.get(col))) {
					rowValue.put(col, SecurityService.getCurrentPsm().getsFName());
				}
				if(":getCurrentDateTime()".equals(rowValue.get(col))) {
					rowValue.put(col, Utils.getCurrentDateTime());
				}
				if(":getCurrentPersonCode()".equals(rowValue.get(col))) {
					rowValue.put(col, SecurityService.getCurrentPerson().getString("cUsercode"));
				}
				if(":getCurrentPersonName()".equals(rowValue.get(col))) {
					rowValue.put(col, SecurityService.getCurrentPerson().getString("cUsername"));
				}
				if(":getCurrentPersonID()".equals(rowValue.get(col))) {
					rowValue.put(col, SecurityService.getCurrentPerson().getString("userId"));
				}
				if(":getCurrentOgnFID()".equals(rowValue.get(col))) {
					rowValue.put(col, SecurityService.getCurrentOgn().getsFID());
				}
				if(":getCurrentOgnLongName()".equals(rowValue.get(col))) {
					rowValue.put(col, SecurityService.getCurrentOgn().getsLongName());
				}
				System.out.println("controller.defaultValues-->>"+col+":"+rowValue.get(col));
			}
			
		}
		return saleOrderService.newData(params);
	}


	@RequestMapping(value={
			"/saleOrder/single/createDetail"
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject saleOrderCreateDetail(@RequestBody JSONObject data) {
		JSONObject json = saleOrderService.newDataDetail(data);
		return json;
	}

	@RequestMapping(value={
			"/saleOrder/single/queryDetail"
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject saleOrderQueryDetail(@RequestBody JSONObject data) {
		return saleOrderService.queryDataDetail(data);
	}

	//整单折扣
	@RequestMapping(value={"/saleOrder/single/discountAmount"},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject saleOrderDiscountAmount(@RequestBody JSONObject data) {
		String id = data.getString("id");
		BigDecimal discountAmount = data.getBigDecimal("discountAmount");
		return saleOrderService.discountAmount(id, discountAmount);
	}
	


	
	//////////////////////////////////////////////////////////////////////////////////////
	@RequestMapping(value={
			"/saleOrder/single/confirm"
			},method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> saleOrderConfirm(@RequestBody JSONObject data) {
		String id = data.getString("id");
		return saleService.saleOrderConfirm(id);
	}

	//////////////////////////////////////////////////////////////////////////////////////
	/*
	@RequestMapping(value={"/diningOrderDetail/single/confirm"},method=RequestMethod.GET)
	@ResponseBody
	public int saleOrderDetailConfirm(@RequestParam(value="id",required=true) String id) {
		return saleOrderDetailService.confirmByMasterID(id);
	}
	*/

	@RequestMapping(value={
			"/saleOrder/single/active"
			},method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> saleOrderActive(@RequestBody JSONObject data) {
		String id = data.getString("id");
		return saleService.saleOrderActive(id);
	}
	
	@RequestMapping(value={
			"/saleOrder/single/agreeIt"
			},method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> saleOrderAgreeIt(@RequestBody JSONObject data) {
		String id = data.getString("id");
		return saleService.saleOrderAgreeIt(id);
	}
	
	/*
	@RequestMapping(value={"/diningOrderDetail/single/agreeIt"},method=RequestMethod.GET)
	@ResponseBody
	public int saleOrderDetailAgreeIt(@RequestParam(value="id",required=true) String id) {
		return saleOrderDetailService.agreeItByMasterID(id);
	}
	*/
	
	@RequestMapping(value={
			"/saleOrder/single/unAgree"
			},method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> saleOrderUnAgree(@RequestBody JSONObject data) {
		String id = data.getString("id");
		return saleService.saleOrderUnAgree(id);
	}
	
	@RequestMapping(value={
			"/saleOrder/single/close"
			},method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> saleOrderClose(@RequestBody JSONObject data) {
		String id = data.getString("id");
		return saleService.saleOrderClose(id);
	}

	@RequestMapping(value={
			"/saleOrder/single/open"
			},method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> saleOrderOpen(@RequestBody JSONObject data) {
		String id = data.getString("id");
		return saleService.saleOrderOpen(id);
	}

	
	@RequestMapping(value={"/saleOrder/single/printPDF"},method=RequestMethod.GET)
	public void saleOrderPrintPDF(@RequestParam(value="id",required=true) String id, HttpServletRequest request, HttpServletResponse response) throws Exception {
		System.out.println("改成封装iText的doc模式，抽出response-->>"+id);
	    Document doc = new Document();// 创建一个PDF文档，我们的PDF内容都写在上面
	    response.setContentType("application/pdf");//在浏览器中打开，使用XMLHttpRequest也可以下载
		PdfWriter.getInstance(doc, response.getOutputStream());
	    //打开doc
	    doc.open();
	    //填写doc内容
		saleService.saleOrderPrintPDF(id,doc);
		//关闭doc
	    doc.close();
	}
	

//	@RequestMapping(value={"/saleOrder/single/downloadPDF"},method=RequestMethod.POST)
	public void saleOrderDownloadPDF(@RequestParam(value="id",required=true) String id, HttpServletRequest request, HttpServletResponse response) throws Exception {
		System.out.println("改成封装iText的doc模式，抽出response-->>"+id);
	    Document doc = new Document();// 创建一个PDF文档，我们的PDF内容都写在上面
	    response.setContentType("application/pdf");//在浏览器中打开，使用XMLHttpRequest也可以下载
	    ///////////////////PDF文件下载//////////////////////////////
//		String realName = id + ".pdf";  
//		response.setContentType("application/octet-stream");
//		response.setHeader("Content-disposition", "attachment; filename=" + new String(realName.getBytes("utf-8"), "ISO8859-1"));
	    /////////////////////////////////////////////////////////////////
		PdfWriter.getInstance(doc, response.getOutputStream());
	    //打开doc
	    doc.open();
	    //填写doc内容
		saleService.saleOrderPrintPDF(id,doc);
		//关闭doc
	    doc.close();
	}
	
	
	//首先打开网页，然后ajax获取数据，再根据数据修改网页，最后打印网页
	@RequestMapping(value={"/saleOrder/single/printWEB"},method=RequestMethod.GET)
	@ResponseBody
	public JSONObject saleOrderPrintWEB(@RequestParam(value="id",required=true) String id, HttpServletRequest request) throws Exception {
		return saleService.saleOrderPrintWEB(id);
	}
	

	//选择销售订单（用于预收款单）
	@RequestMapping(value="/saleOrder/selectSingle",method=RequestMethod.GET)
	public ModelAndView saleOrderSelectSingle(){
		ModelAndView mav = new ModelAndView("/appcenter/sale/saleOrder/selectSingle");
		return mav;
	}
	
	//选择销售订单（用于预收款单）
//	@RequestMapping(value="/saleOrder/selectMulti",method=RequestMethod.GET)
//	public ModelAndView saleOrderSelectMulti(){
//		ModelAndView mav = new ModelAndView("/appcenter/sale/saleOrder/selectMulti");
//		return mav;
//	}

	
	@RequestMapping(value="/orderDetailQuery/list",method=RequestMethod.GET)
	public ModelAndView saleOrderDetailQueryList(){
		ModelAndView mav = new ModelAndView("/appcenter/sale/orderDetailQuery/list");
		mav.addObject("path", "/sale/orderDetailQuery/list");
		return mav;
	}

	@RequestMapping(value="/orderDetailQuery/list/query",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject saleOrderDetailQuery(@RequestBody JSONObject data,HttpServletRequest request) {
		/*
		//获取访问路径
		String url = request.getRequestURI().replaceAll(request.getContextPath(), "").replaceAll("/query", "");
		log.info("url-->"+url);
		//判断是否具有只查看自己的条件
		if("Y".equals(SecurityService.getDataAccessPerm(url))){
			data.put("userId", SecurityService.getCurrentPerson().getsID());
		}
		
		String enterpriseNo = SecurityService.getCurrentOgn().getsFID();
		data.put("enterpriseNo", enterpriseNo);
		*/
		data = SecurityService.addDataAccessPerm(data);
		return saleOrderService.queryDataDetail(data);
	}

	//选择销售订单明细表（用于发货单）
	@RequestMapping(value="/orderDetailSelect/selectMulti",method=RequestMethod.GET)
	public ModelAndView saleOrderDetailSelectMulti(){
		ModelAndView mav = new ModelAndView("/appcenter/sale/orderDetailSelect/selectMulti");
		return mav;
	}
	

	//待审核单据数量
	@RequestMapping(value="/saleOrder/getUnAgreeCount",method=RequestMethod.GET)
	@ResponseBody
	public JSONObject getUnAgreeCount() {
		JSONObject json = new JSONObject();
		
		if(SecurityService.getCurrentOgn()==null){
			//实际上下面这一段不会被执行，因为shiro会首先拦截掉未登录的请求，重定向到未授权页面。
			json.put("code", 403);
			json.put("msg", "未登录");
			return json;
			
		}else {
			String enterpriseNo = SecurityService.getCurrentOgn().getsFID();
			long count = saleOrderService.getUnAgreeCount(enterpriseNo);
			json.put("code", 0);
			json.put("msg", "success");
			json.put("data", count);
			return json;
		}
		
	}
	
	
	
	
	
	


	
	
	//////////////////////////////////////////////////////////////////////////////////////

	/*
	@RequestMapping(value="/saleBom/single/confirm",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> saleBomConfirm(@RequestBody JSONObject data) {
		String id = data.getString("id");
		return productService.saleBomConfirm(id);
	}

	@RequestMapping(value="/saleBom/single/active",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> saleBomActive(@RequestBody JSONObject data) {
		String id = data.getString("id");
		return productService.saleBomActive(id);
	}
	@RequestMapping(value="/saleBom/single/agreeIt",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> saleBomAgreeIt(@RequestBody JSONObject data) {
		String id = data.getString("id");
		return productService.saleBomAgreeIt(id);
	}

	@RequestMapping(value="/saleBom/single/unAgree",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> saleBomUnAgree(@RequestBody JSONObject data) {
		String id = data.getString("id");
		return productService.saleBomUnAgree(id);
	}
	@RequestMapping(value="/saleBom/single/close",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> saleBomClose(@RequestBody JSONObject data) {
		String id = data.getString("id");
		return productService.saleBomClose(id);
	}

	@RequestMapping(value="/saleBom/single/open",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> saleBomOpen(@RequestBody JSONObject data) {
		String id = data.getString("id");
		return productService.saleBomOpen(id);
	}
	*/

	//////////////////////////////////////////////////////////////////////////////////////

	

	
	
	//销售发货
	@RequestMapping(value="/invXSCK/single",method=RequestMethod.GET)
	public ModelAndView invXSCKSingle(){
		ModelAndView mav = new ModelAndView("/appcenter/sale/invXSCK/single");
		mav.addObject("path", "/sale/invXSCK/single");
		return mav;
	}
	
	//销售退货
	@RequestMapping(value="/invXSRK/single",method=RequestMethod.GET)
	public ModelAndView invXSRKSingle(){
		ModelAndView mav = new ModelAndView("/appcenter/sale/invXSRK/single");
		mav.addObject("path", "/sale/invXSRK/single");
		return mav;
	}
	
	
	@RequestMapping(value={
			"/invXSCK/single/query"
			,"/invXSRK/single/query"
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject invXSCKQuery(@RequestBody JSONObject data) {
		data = SecurityService.addDataAccessPerm(data);
		return storageRecordService.queryData(data);
	}

	@RequestMapping(value={
			"/invXSCK/single/save"
			,"/invXSRK/single/save"
	},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject invXSCKSave(@RequestBody JSONObject object) {
		return storageRecordService.saveData(object);
	}

	@RequestMapping(value={
			"/invXSCK/single/delete"
			,"/invXSRK/single/delete"
	},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject invXSCKDelete(@RequestBody JSONObject data) {
		return storageRecordService.deleteData(data);
	}

	@RequestMapping(value={
			"/invXSCK/single/create"
			,"/invXSRK/single/create"
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject invXSCKCreate(@RequestBody JSONObject data, HttpServletRequest request) {
		data = SecurityService.translateFun(data);
		return storageRecordService.newData(data);
	}

	@RequestMapping(value={
			"/invXSCK/single/createDetail"
			,"/invXSRK/single/createDetail"
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject invXSCKCreateDetail(@RequestBody JSONObject data, HttpServletRequest request) {
		return storageRecordService.newDataDetail(data);
	}

	@RequestMapping(value={
			"/invXSCK/single/queryDetail"
			,"/invXSRK/single/queryDetail"
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject invXSCKQueryDetail(@RequestBody JSONObject data) {
		return storageRecordService.queryDataDetail(data);
	}


	//整单折扣
	@RequestMapping(value={"/invXSCK/single/discountAmount"},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject invXSCKDiscountAmount(@RequestBody JSONObject data) {
		String id = data.getString("id");
		BigDecimal discountAmount = data.getBigDecimal("discountAmount");
		return storageRecordService.discountAmount(id, discountAmount);
	}
	

	
	//////////////////////////////////////////////////////////////////////////////////////
	@RequestMapping(value={
			"/invXSCK/single/confirm"
			,"/invXSRK/single/confirm"
			},method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> invXSCKConfirm(@RequestBody JSONObject data) {
		String id = data.getString("id");
		String tUsercode = SecurityService.getCurrentPerson().getString("cUsercode");
		String tUsername = SecurityService.getCurrentPerson().getString("cUsername");
		String userId = SecurityService.getCurrentPerson().getString("userId");

		String employeeNo = SecurityService.getCurrentPsm().getsFID();
		String employeeName = SecurityService.getCurrentPsm().getsFName();
		
		return saleService.storageRecordConfirm(id,tUsercode,tUsername,userId,employeeNo,employeeName);
	}

	@RequestMapping(value={
			"/invXSCK/single/active"
			,"/invXSRK/single/active"
			},method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> invXSCKActive(@RequestBody JSONObject data) {
		String id = data.getString("id");
		
		String cUsercode = SecurityService.getCurrentPerson().getString("cUsercode");
		String cUsername = SecurityService.getCurrentPerson().getString("cUsername");
		String userId = SecurityService.getCurrentPerson().getString("userId");
		
		String employeeNo = SecurityService.getCurrentPsm().getsFID();
		String employeeName = SecurityService.getCurrentPsm().getsFName();

		return saleService.storageRecordActive(id, cUsercode, cUsername, userId, employeeNo, employeeName);
	}
	
	
	@RequestMapping(value={
			"/invXSCK/single/agreeIt"
			,"/invXSRK/single/agreeIt"
	},method=RequestMethod.POST)
	
	@ResponseBody
	public  Map<String,Object> invXSCKAgreeIt(@RequestBody JSONObject data) {
		String id = data.getString("id");
		String tUsercode = SecurityService.getCurrentPerson().getString("cUsercode");
		String tUsername = SecurityService.getCurrentPerson().getString("cUsername");
		String userId = SecurityService.getCurrentPerson().getString("userId");

		String employeeNo = SecurityService.getCurrentPsm().getsFID();
		String employeeName = SecurityService.getCurrentPsm().getsFName();
		
		return saleService.storageRecordAgreeIt(id,tUsercode,tUsername,userId,employeeNo,employeeName);
	}

	@RequestMapping(value={
			"/invXSCK/single/unAgree"
			,"/invXSRK/single/unAgree"
			},method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> invXSCKUnAgree(@RequestBody JSONObject data) {
		String id = data.getString("id");
		
		String tUsercode = SecurityService.getCurrentPerson().getString("cUsercode");
		String tUsername = SecurityService.getCurrentPerson().getString("cUsername");
		String userId = SecurityService.getCurrentPerson().getString("userId");
		
		String employeeNo = SecurityService.getCurrentPsm().getsFID();
		String employeeName = SecurityService.getCurrentPsm().getsFName();

		return saleService.storageRecordUnAgree(id,tUsercode,tUsername,userId,employeeNo,employeeName);
	}
	
	@RequestMapping(value={
			"/invXSCK/single/close"
			,"/invXSRK/single/close"
			},method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> invXSCKClose(@RequestBody JSONObject data) {
		String id = data.getString("id");
		
		String cUsercode = SecurityService.getCurrentPerson().getString("cUsercode");
		String cUsername = SecurityService.getCurrentPerson().getString("cUsername");
		String userId = SecurityService.getCurrentPerson().getString("userId");
		
		String employeeNo = SecurityService.getCurrentPsm().getsFID();
		String employeeName = SecurityService.getCurrentPsm().getsFName();
		return saleService.storageRecordClose(id, cUsercode, cUsername, userId, employeeNo, employeeName);
	}

	@RequestMapping(value={
			"/invXSCK/single/open"
			,"/invXSRK/single/open"
			},method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> invXSCKOpen(@RequestBody JSONObject data) {
		String id = data.getString("id");
		
		String cUsercode = SecurityService.getCurrentPerson().getString("cUsercode");
		String cUsername = SecurityService.getCurrentPerson().getString("cUsername");
		String userId = SecurityService.getCurrentPerson().getString("userId");
		
		String employeeNo = SecurityService.getCurrentPsm().getsFID();
		String employeeName = SecurityService.getCurrentPsm().getsFName();
		return saleService.storageRecordOpen(id, cUsercode, cUsername, userId, employeeNo, employeeName);
	}

	//打印销售发货单
	@RequestMapping(value={
			"/invXSCK/single/printPDF"
			},method=RequestMethod.GET)
	public void invXSCKPrintPDF(@RequestParam(value="id",required=true) String id, HttpServletResponse response) throws Exception {
		//storageService.invXSCKPrintPDF(id, response);
	    Document doc = new Document();// 创建一个PDF文档，我们的PDF内容都写在上面
	    response.setContentType("application/pdf");
//	    PdfWriter writer = PdfWriter.getInstance(doc, response.getOutputStream());
	    PdfWriter.getInstance(doc, response.getOutputStream());
	    //打开doc
	    doc.open();
	    //填写doc内容
	    saleService.invXSCKPrintPDF(id,doc);
		//关闭doc
	    doc.close();
	}

	//打印销售退货单
	@RequestMapping(value={
			"/invXSRK/single/printPDF"
			},method=RequestMethod.GET)
	public void invXSRKPrintPDF(@RequestParam(value="id",required=true) String id, HttpServletResponse response) throws Exception {
		//storageService.invCGRKPrintPDF(id, response);
	    Document doc = new Document();// 创建一个PDF文档，我们的PDF内容都写在上面
	    response.setContentType("application/pdf");
//	    PdfWriter writer = PdfWriter.getInstance(doc, response.getOutputStream());
	    PdfWriter.getInstance(doc, response.getOutputStream());
	    //打开doc
	    doc.open();
	    //填写doc内容
	    saleService.invXSRKPrintPDF(id,doc);
		//关闭doc
	    doc.close();
	}
	
	//由销售订单生成销售发货单
	@RequestMapping(value = "/saleOrder/single/pushXSCK", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject pushXSCK(@RequestBody JSONObject params, HttpServletRequest request){
		String id = params.getString("id");
		
		SaleOrder saleOrder = saleOrderService.getByID(id);
		
		String cUsercode = SecurityService.getCurrentPerson().getString("cUsercode");
		String cUsername = SecurityService.getCurrentPerson().getString("cUsername");
		String userId = SecurityService.getCurrentPerson().getString("userId");
		
		String employeeNo = SecurityService.getCurrentPsm().getsFID();
		String employeeName = SecurityService.getCurrentPsm().getsFName();
		
		int flowType = 1;
		
//		String orderType = "PTXS";
//		String orderTypeLabel = "普通销售";
		
		//int pickFlag = 0;//不需要拣货操作

		//填写销售发货单
		StorageRecord obj = saleService.storageRecordXSCKInsert(saleOrder, flowType, cUsercode, cUsername, userId, employeeNo, employeeName);

				
		/*
		JSONObject json = new JSONObject();
		if(obj==null){
			json.put("code", -1);
			json.put("status", "生成单据失败");
			return json;
		}else{
			json.put("code", 0);
			json.put("status", "成功");
			json.put("billNo", obj.getRecordNo());
		}
		*/

		MapMessage mapMessage = null;
		
		if(obj==null){
			mapMessage = new MapMessage(null,null,null,null
					,MapMessage.STATUSINFO_FAILURE_CODE//"http://www.iune.cn"
					,MapMessage.STATUSINFO_FAILURE_MSG//"操作成功！"
					,MapMessage.STATUSINFO_FAILURE_INFO//"操作成功！"
					,MapMessage.STATUSINFO_FAILURE_FLAG//"success"
					);			
			return mapMessage.toJSONObject();
		}
		
		mapMessage = new MapMessage(obj.getfID(),obj.getRecordNo(),obj.getBillType(),obj.getBillTypeName()
				,MapMessage.STATUSINFO_SUCCESS_CODE//"http://www.iune.cn"
				,MapMessage.STATUSINFO_SUCCESS_MSG//"操作成功！"
				,MapMessage.STATUSINFO_SUCCESS_INFO//"操作成功！"
				,MapMessage.STATUSINFO_SUCCESS_FLAG//"success"
				);
		
		SysInfo sysInfo = sysInfoService.getByID(obj.getSystemCode());
		if(sysInfo!=null){
			mapMessage.setSystemCode(sysInfo.getfID());
			mapMessage.setsProcess(sysInfo.getAbsoluteUrl());
			mapMessage.setsName(sysInfo.getSystemName());
		}
		
		return mapMessage.toJSONObject();
		
	}
	
	//退货单
	@RequestMapping(value={"/invXSCK/single/pushXSRK"},method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> pushXSRK(@RequestBody JSONObject params, HttpServletRequest request) {
		String cUsercode = SecurityService.getCurrentPerson().getString("cUsercode");
		String cUsername = SecurityService.getCurrentPerson().getString("cUsername");
		String userId = SecurityService.getCurrentPerson().getString("userId");

		String employeeNo = SecurityService.getCurrentPsm().getsFID();
		String employeeName = SecurityService.getCurrentPsm().getsFName();
		
		String id = params.getString("id");
		
		//填写销售发货单
		StorageRecord storageRecord = storageRecordService.getByID(id);
		
		String autoState = "N";
		int flowType = 1;
		String autoPayment = "N";//现结
		
		//填写销售发货单
		StorageRecord obj = saleService.storageRecordBACKInsert(storageRecord, cUsercode, cUsername, userId, autoState, flowType, autoPayment, employeeNo, employeeName);

		MapMessage mapMessage = null;
		
		if(obj==null){
//			JSONObject json = new JSONObject();
//			json.put("code", -1);
//			json.put("status", "生成单据失败");
//			return json;
			
			mapMessage = new MapMessage(null,null,null,null
					,MapMessage.STATUSINFO_FAILURE_CODE//"http://www.iune.cn"
					,MapMessage.STATUSINFO_FAILURE_MSG//"操作成功！"
					,MapMessage.STATUSINFO_FAILURE_INFO//"操作成功！"
					,MapMessage.STATUSINFO_FAILURE_FLAG//"success"
					);			
			return mapMessage.toJSONObject();
		}
		
//		JSONObject json = new JSONObject();
//		json.put("code", 0);
//		json.put("status", "成功");
//		json.put("id", accountPayment.getfID());
//		json.put("billNo", accountPayment.getAccountPaymentNo());
//		return json;
		
		mapMessage = new MapMessage(obj.getfID(),obj.getRecordNo(),obj.getBillType(),obj.getBillTypeName()
				,MapMessage.STATUSINFO_SUCCESS_CODE//"http://www.iune.cn"
				,MapMessage.STATUSINFO_SUCCESS_MSG//"操作成功！"
				,MapMessage.STATUSINFO_SUCCESS_INFO//"操作成功！"
				,MapMessage.STATUSINFO_SUCCESS_FLAG//"success"
				);
		
		SysInfo sysInfo = sysInfoService.getByID(obj.getSystemCode());
		if(sysInfo!=null){
			mapMessage.setSystemCode(sysInfo.getfID());
			mapMessage.setsProcess(sysInfo.getAbsoluteUrl());
			mapMessage.setsName(sysInfo.getSystemName());
		}

		return mapMessage.toJSONObject();
		
//		return storageService.pushBACK(params, billType, billTypeName, systemCode, flowType, revOpay, direction, costingCode, autoCode, origin, originName, billNo, cUsercode, cUsername, userId, autoState, autoPayment, employeeNo, employeeName);
	}
	
	
	//由销售发货单生成销售发票
	@RequestMapping(value = "/invXSCK/single/pushXSFP", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject pushXSFP(@RequestBody JSONObject params, HttpServletRequest request){
		String id = params.getString("rowID");
		StorageRecord storageRecord = storageRecordService.getByID(id);
		
		String cUsercode = SecurityService.getCurrentPerson().getString("cUsercode");
		String cUsername = SecurityService.getCurrentPerson().getString("cUsername");
		String userId = SecurityService.getCurrentPerson().getString("userId");
		
		String employeeNo = SecurityService.getCurrentPsm().getsFID();
		String employeeName = SecurityService.getCurrentPsm().getsFName();
		
		int flowType = 1;//
		int cashSettle = 0;//现结
		
		//填写付款单付款
		AccountInvoice obj = invoiceService.accountInvoiceInsert(storageRecord, flowType, cashSettle, cUsercode, cUsername, userId, employeeNo, employeeName);
		
		/*
		JSONObject json = new JSONObject();
		if(obj!=null){
			json.put("code", 0);
			json.put("status", "成功");
			json.put("zdh", obj.getAccountInvoiceNo());
		}else{
			json.put("code", -1);
			json.put("status", "失败");
			
		}
		
		return json;
		*/

		MapMessage mapMessage = null;
		JSONObject json = new JSONObject();
		
		if(obj==null){
			mapMessage = new MapMessage(null,null,null,null
					,MapMessage.STATUSINFO_FAILURE_CODE//"http://www.iune.cn"
					,MapMessage.STATUSINFO_FAILURE_MSG//"操作成功！"
					,MapMessage.STATUSINFO_FAILURE_INFO//"操作成功！"
					,MapMessage.STATUSINFO_FAILURE_FLAG//"success"
					);			
			json =  mapMessage.toJSONObject();
			json.put("status", "失败");
			
			return json;
		}
		
		mapMessage = new MapMessage(obj.getfID(),obj.getAccountInvoiceNo(),obj.getBillType(),obj.getBillTypeName()
				,MapMessage.STATUSINFO_SUCCESS_CODE//"http://www.iune.cn"
				,MapMessage.STATUSINFO_SUCCESS_MSG//"操作成功！"
				,MapMessage.STATUSINFO_SUCCESS_INFO//"操作成功！"
				,MapMessage.STATUSINFO_SUCCESS_FLAG//"success"
				);
		
		SysInfo sysInfo = sysInfoService.getByID(obj.getSystemCode());
		if(sysInfo!=null){
			mapMessage.setSystemCode(sysInfo.getfID());
			mapMessage.setsProcess(sysInfo.getAbsoluteUrl());
			mapMessage.setsName(sysInfo.getSystemName());
		}
		
		json = mapMessage.toJSONObject();
		json.put("status", "成功");
		json.put("zdh", obj.getAccountInvoiceNo());
		
		return json;
		
	}
	

	//////////////////////////////////////////////////////////////////////////////////////
	
	//销售发货明细表
	@RequestMapping(value="/invXSCKDetailQuery/list",method=RequestMethod.GET)
	public ModelAndView invXSCKDetailQuery(){
		ModelAndView mav = new ModelAndView("/appcenter/sale/invXSCKDetailQuery/list");
		return mav;
	}
	
	//销售退货明细表
	@RequestMapping(value="/invXSRKDetailQuery/list",method=RequestMethod.GET)
	public ModelAndView invXSRKDetailQuery(){
		ModelAndView mav = new ModelAndView("/appcenter/sale/invXSRKDetailQuery/list");
		return mav;
	}

	@RequestMapping(value= {
			"/invXSCKDetailQuery/list/query"
			,"/invXSRKDetailQuery/list/query"
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject recordDetailQuery(@RequestBody JSONObject data,HttpServletRequest request) {
		data = SecurityService.addDataAccessPerm(data);
		return storageRecordService.queryDataDetail(data);
	}
	
	//选择销售发货单
	@RequestMapping(value="/invXSCK/selectSingle",method=RequestMethod.GET)
	public ModelAndView invXSCKSelectSingle(){
		ModelAndView mav = new ModelAndView("/appcenter/sale/invXSCK/selectSingle");
		return mav;
	}
	

	
	
	
	//销售管理面板
	@RequestMapping(value="/saleReport/dashboard",method=RequestMethod.GET)
	public ModelAndView saleReport(){
		ModelAndView mav = new ModelAndView("/appcenter/sale/saleReport/dashboard");
		mav.addObject("path", "/sale/saleReport/dashboard");
		return mav;
	}
	




}
